package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Question;
import com.ruoyi.system.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/system/question")
public class QuestionController extends BaseController {
    @Autowired
    private IQuestionService questionService;

    @GetMapping("/list")
    public TableDataInfo list(Question question) {
        startPage();
        List<Question> list = questionService.selectQuestionList(question);
        //List<Question> list = new ArrayList<>();
        //list.add(question);
        return getDataTable(list);
    }

    @GetMapping("/bybank/{bankId}")
    public TableDataInfo getQuestionsByBankId(@PathVariable Long bankId) {
        startPage();
        List<Question> list = questionService.selectQuestionListByBankId(bankId);
        return getDataTable(list);
    }


    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable Long questionId)
    {
        return success(questionService.selectQuestionByQuestionId(questionId));
    }

    @PostMapping
    public AjaxResult add(@RequestBody Question question) {
        questionService.addQuestion(question);
        return AjaxResult.success();
    }


    /**
     * 修改题目
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Question question)
    {
        return toAjax(questionService.updateQuestion(question));
    }

    /**
     * 删除题目
     */
    @DeleteMapping("/{questionId}")
    public AjaxResult delete(@PathVariable Integer questionId) {
        return toAjax(questionService.deleteQuestionById(questionId));
    }
}
